import {getRequest} from './api';

export const initMenu = (router,store) => {
    if(store.state.routes.length > 0){
        return;
    }
    getRequest('/menu/menu').then( data => {
        if(data){
            //格式化后的router
            let fmtRoutes = formatRoutes(data);
            console.log(fmtRoutes);
            //添加到路由
            //router.addRoutes(fmtRoutes);
            
            //添加到vuex
            store.commit('initRoutes',fmtRoutes);
        }
    })
}

export const formatRoutes = (routes) => {
    let fmtRoutes = [];


    routes.forEach( route => {
        let {path,component,name,iconCls,children} = route;
        //递归
        if( children && children instanceof Array){
            children = formatRoutes(children);
        }

        let fmRouter = {
            path:path,
            name:name,
            iconCls: iconCls,
            children: children,
            component(resolve){
                if( path.startsWith("/admin/user/")){
                    require(['../views/admin/user/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/subject/")){
                    require(['../views/admin/subject/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/exam/")){
                    require(['../views/admin/exam/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/question/")){
                    require(['../views/admin/question/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/task/")){
                    require(['../views/admin/task/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/correcting/")){
                    require(['../views/admin/correcting/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/information/")){
                    require(['../views/admin/information/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/log/")){
                    require(['../views/admin/log/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/knowledge/")){
                    require(['../views/admin/knowledge/'+component+'.vue'],resolve);
                }else if( path.startsWith("/admin/system/")){
                    require(['../views/admin/system/'+component+'.vue'],resolve);
                }else if( path.startsWith("/teacher/correcting/")){
                    require(['../views/teacher/correcting/'+component+'.vue'],resolve);
                }else if( path.startsWith("/teacher/question/")){
                    require(['../views/teacher/question/'+component+'.vue'],resolve);
                }else if( path.startsWith("/teacher/exam/")){
                    require(['../views/teacher/exam/'+component+'.vue'],resolve);
                }else if( path.startsWith("/teacher/clazz/")){
                    require(['../views/teacher/clazz/'+component+'.vue'],resolve);
                }else if( path.startsWith("/teacher/performance/")){
                    require(['../views/teacher/performance/'+component+'.vue'],resolve);
                }else if( path.startsWith("/teacher/user/")){
                    require(['../views/teacher/user/'+component+'.vue'],resolve);
                }
            }
        }
        
        fmtRoutes.push(fmRouter);
    })
    return fmtRoutes;
}